﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using frontend_wcf.WCFService;
using System.IO;

namespace frontend_wcf
{
    public partial class FileListItem : UserControl
    {
        ServiceClient service;
        FrmMain main;
        private DummyFile file;
        private UserType userType;

        public FileListItem(ServiceClient service, FrmMain main, DummyFile file, UserType userType)
        {
            InitializeComponent();
            this.main = main;
            this.service = service;
            this.file = file;
            this.userType = userType;
            lbl_FileName.Text = this.file.name;
            lbl_FileSize.Text = string.Format("{0} b", this.file.size);

            if (userType != UserType.admin)
            {
                btn_Delete.Hide();
            }
            if (userType != UserType.admin && userType != UserType.normal)
            {
                btn_Download.Hide();
            }
        }


        private void btn_Download_Click(object sender, EventArgs e)
        {
            saveFileDialog.FileName = file.name;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string path = saveFileDialog.FileName;
                byte[] rawData = service.getFile(file.id);
                FileStream fs;
                fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                fs.Write(rawData, 0, rawData.Length);
                fs.Close();
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            service.deleteFile(file.id);
            main.reloadFiles();
        }
    }
}
